﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    /// <summary>
    /// Interaction logic for AnimationContainerView.xaml
    /// </summary>
    public partial class AnimationContainerView : UserControl, INotifyPropertyChanged
    {
        private AnimationContainerViewModel _viewModel;
        public AnimationSelectionInfo AnimationSelectionInfo { get { return ViewModel.AnimationSelectionInfo; } }

        public event PropertyChangedEventHandler PropertyChanged;

        public AnimationContainerView()
        {
            InitializeComponent();
            ViewModel = new AnimationContainerViewModel();
        }

        public void LoadResources(NotifiableCollection<ResourceData> animationResourceCollection)
        {
            ViewModel.LoadResources(animationResourceCollection);
        }

        private IEnumerable<FrameData> GetSelectedFrames()
        {
            ListBoxItem listBoxItem = animationLayerListBox.ItemContainerGenerator.ContainerFromItem(animationLayerListBox.SelectedItem) as ListBoxItem;
            var listBox = PavilionVisualTreeHelper.GetChild<ListBox>(listBoxItem);

            object[] array = new object[listBox.SelectedItems.Count];
            listBox.SelectedItems.CopyTo(array, 0);

            return array.Select((x) => x as FrameData);
        }

        public AnimationContainerViewModel ViewModel
        {
            get { return _viewModel; }
            set { _viewModel = value; this.DataContext = _viewModel; }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ViewModel.UpdateSelectionInfo();
        }

        private void FrameItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ListBox frameListBox = GetParentListBox((DependencyObject)sender);
            ListBox AnimationListBox = GetParentListBox(frameListBox);

            AnimationListBox.SelectedItem = (AnimationLayer)frameListBox.DataContext;
        }

        private ListBox GetParentListBox(DependencyObject dependencyObject)
        {
            DependencyObject parent = VisualTreeHelper.GetParent((DependencyObject)dependencyObject);
            while (!(parent is ListBox))
                parent = VisualTreeHelper.GetParent(parent);

            return (ListBox)parent;
        }

        private void AddFrameButton_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.CreateFrame();
        }

        private void FrameListBox_RemoveFrameItem_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.RemoveFrames(GetSelectedFrames());
        }

        private void AddLayerMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.CreateAnimationLayer();
        }

        private void RemoveAnimationLayer_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.RemoveAnimationLayer((AnimationLayer)((FrameworkElement)sender).DataContext);
        }

        private void CopyFrameMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.CopySelectedFrames(GetSelectedFrames());
        }

        private void PasteFrameMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.PasteFrames();
        }

        private void AnimationLayerVisibilityMenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;

            if (menuItem.Header.ToString() == "Hide")
            {
                ViewModel.CurrentAnimationLayer.IsEnabled = false;
                menuItem.Header = "Show";
            }
            else
            {
                ViewModel.CurrentAnimationLayer.IsEnabled = true;
                menuItem.Header = "Hide";
            }
        }
    }
}
